﻿using System;
using System.Web.Mvc;
using System.Web.Security;
using Kaiser.ManageClient.Areas.Portal.Models;
using Kaiser.ManageClient.Resource;
using Kaiser.MvcBase.Context;
using Kaiser.MvcBase.ControllerBase;
using Kaiser.MvcBase.Models;
using Kaiser.MvcBase.Service;
using Kaiser.Utilities.Captcha;
using Kaiser.Utils;
using Microsoft.Practices.Unity;

namespace Kaiser.ManageClient.Areas.Portal.Controllers
{
    public class PassportController : Controller
    {
        [Dependency]
        public IAuthenUserService UserService { get; set; }

        //
        // GET: /Account/Login

        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            //todo for login easily
            return View(new LoginModel() {UserName = "tomcat", Password = "100001"});
            //return View();
        }

        //
        // POST: /Account/Login

        [HttpPost]
        [AllowAnonymous]
        //[ValidateAntiForgeryToken]
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            var tmp = Session[CaptchaUtil.DefaultKey];
            if (tmp == null)
            {
                ModelState.AddModelError("ValidateCode", UIMessages.PassportController_Login_ValidateCodeOverTime);
                return View(model);
            }
            var code = tmp.ToString();
            //todo for login easily
            bool validateCodeResult = code.ToUpper() == model.ValidateCode.ToUpper();
            if (!validateCodeResult)
            {
                ModelState.AddModelError("ValidateCode", UIMessages.PassportController_Login_ValidateCodeError);
                return View();
            }
            ;
            if (ModelState.IsValid)
            {
           
                UserModel user;
                var result = ManageContext.UserService.Login(model.UserName, model.Password, out user);
                if (result)
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
 
                    if (!String.IsNullOrEmpty(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    if (string.IsNullOrEmpty(returnUrl))
                        return RedirectToAction("Index", "Home");
                    return Redirect(returnUrl);
                }
                //如果我们进行到这一步时某个地方出错，则重新显示表单
                ModelState.AddModelError("", UIMessages.PassportController_Login_UserNameOrPasswordError);
            }
            else
            {
                ModelState.AddModelError("", UIMessages.PassportController_Login_UserNameOrPasswordError);
            }

            return View(model);
        }

        [AllowAnonymous]
        //[ValidateAntiForgeryToken]
        public ActionResult Logout(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            FormsAuthentication.SignOut();
            return Redirect("Login");
        }

        [AllowAnonymous]
        public ActionResult CreateValidateImage()
        {
            var validateCode = ValidateCodeHelper.CreateCode(4);
            Session["ValidateCode"] = validateCode;
            var image = ValidateCodeHelper.CreateValidateGraphic(validateCode);
            return File(image, @"image/jpeg");
        }

        [HttpPost]
        public JsonResult ChangePassword(string account, string newpassword)
        {
            object result;

            try
            {
                //FacadeFactory.CreateUserFacade().ModifyPassword(account, newpassword);

                result = new {success = true, msg = "修改成功!"};
            }
            catch (Exception ex)
            {
                result = new {success = false, msg = ex.Message};

            }
            return Json(result);
        }

        [HttpPost]
        public JsonResult ChangeSelfPassword(string account, string oldpassword, string newpassword)
        {
            object result;

            try
            {
                //FacadeFactory.CreateUserFacade().ModifyPassword(account, oldpassword, newpassword);

                result = new {success = true, msg = "修改成功!"};
            }
            catch (Exception ex)
            {
                result = new {success = false, msg = ex.Message};

            }
            return Json(result);
        }
    }
}
